/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package adee.qrlogin;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
import java.util.List;


/**
 *
 * @author adee
 */
public class LoginManager {
    private Activity context;
    public  final int QR_READER = 0;
    public  final int ACCOUNT_FORM = 1;
    private AccountsDB db;

    public LoginManager(Activity context) {
        this.context = context;
        this.db = new AccountsDB(context);
    }



    public void openQRReader() {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        context.startActivityForResult(intent, this.QR_READER);
    }

    public void openNewAccountForm(String link,String sessionid){
       Intent   i = new Intent(context, AccountFormActivity.class);
       i.putExtra("link", link);
       i.putExtra("sessionid",sessionid);
       context.startActivityForResult(i,this.ACCOUNT_FORM);
    }

    public void openEditAccountForm(String id){
       Intent   i = new Intent(context, AccountFormActivity.class);
       i.putExtra("id", id);
       context.startActivityForResult(i,this.ACCOUNT_FORM);
    }


    public void onActivityResult(int requestCode, int resultCode, Intent intent){
       if (requestCode == this.QR_READER) {
           if (resultCode == Activity.RESULT_OK) {
               String result = intent.getStringExtra("SCAN_RESULT");
               String temp[] = result.split("\n");
               String link = temp[1];
               String session = temp[0];
               Account account = db.getAccountByLink(link);
             
        
               if (account!=null){
                   account.setSessionId(session);
                   this.doLogin(account);
               }
                  else this.openNewAccountForm(link,session);


           } else if (resultCode == Activity.RESULT_CANCELED) {
            // Handle cancel
           }
       }

       if (requestCode==this.ACCOUNT_FORM){
           if (resultCode == Activity.RESULT_OK) {
             String id = intent.getStringExtra("id");
             Account account = db.getAccount(Integer.parseInt(id));
             this.doLogin(account);

           } else if (resultCode == Activity.RESULT_CANCELED) {
            // Handle cancel
           }


       }
     }

    public void doLogin(Account account){
          String respond = account.login();

         if (account.isLogined()==1){
            Toast.makeText(context,respond,Toast.LENGTH_LONG);
            db.saveAccount(account);

         }
         else {
              Toast.makeText(context,respond,Toast.LENGTH_LONG);
              this.openEditAccountForm(String.valueOf(account.getId()));
         }

    }


    private void beforeLogin(){
        this.openQRReader();
    }

    private void login(){

    }

    private void afterLogin(){
        
    }


    public void startLoginProcedure(){
        beforeLogin();
    }


    public void logout(Account account){
          account.logout();
          this.db.saveAccount(account);
    }

    public void logoutAll(){
        List<Account> accounts = this.db.getAccounts();
        for(Account account: accounts){
            this.logout(account);

        }
    }

    public void refreshAll(){
           List<Account> accounts = this.db.getAccounts();
            for(Account account: accounts){
               account.refreshStatus();
            }

           this.db.saveAccounts(accounts);
    }

}
